home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / stream.cc < prev    next >
C/C++ Source or Header  |  1993-10-23  |  4KB  |  145 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #include <stdarg.h>
  26. #include "libioP.h"
  27. #include "stream.h"
  28. #include "strstream.h"
  29.  
  30. static char Buffer[_IO_BUFSIZ];
  31. #define EndBuffer (Buffer+_IO_BUFSIZ)
  32. static char* next_chunk = Buffer; // Start of available part of Buffer.
  33.  
  34. char* form(const char* format, ...)
  35. {
  36.     int space_left = EndBuffer - next_chunk;
  37.     // If less that 25% of the space is available start over.
  38.     if (space_left < (_IO_BUFSIZ>>2))
  39.     next_chunk = Buffer;
  40.     char* buf = next_chunk;
  41.  
  42.     strstreambuf stream(buf, EndBuffer-buf-1, buf);
  43.     va_list ap;
  44.     va_start(ap, format);
  45.     int count = stream.vform(format, ap);
  46.     va_end(ap);
  47.     stream.sputc(0);
  48.     next_chunk = buf + stream.pcount();
  49.     return buf;
  50. }
  51.  
  52. #define u_long unsigned long
  53.  
  54. static char* itoa(unsigned long i, int size, int neg, int base)
  55. {
  56.     // Conservative estimate: If base==2, might need 8 characters
  57.     // for each input byte, but normally 3 is plenty.
  58.     int needed = size ? size
  59.     : (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
  60.     int space_left = EndBuffer - next_chunk;
  61.     if (space_left <= needed)
  62.     next_chunk = Buffer; // start over.
  63.  
  64.     char* buf = next_chunk;
  65.  
  66.     register char* ptr = buf+needed+1;
  67.     next_chunk = ptr;
  68.  
  69.     if (needed < (2+neg) || ptr > EndBuffer)
  70.     return NULL;
  71.     *--ptr = 0;
  72.     
  73.     if (i == 0)
  74.     *--ptr = '0';
  75.     while (i != 0 && ptr > buf) {
  76.     int ch = i % base;
  77.     i = i / base;
  78.     if (ch >= 10)
  79.         ch += 'a' - 10;
  80.     else
  81.         ch += '0';
  82.     *--ptr = ch;
  83.     }
  84.     if (neg)
  85.     *--ptr = '-';
  86.     if (size == 0)
  87.     return ptr;
  88.     while (ptr > buf)
  89.     *--ptr = ' ';
  90.     return buf;
  91. }
  92.  
  93. char* dec(long i, int len /* = 0 */)
  94. {
  95.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  96.     else return itoa((unsigned long)(-i), len, 1, 10);
  97. }
  98. char* dec(int i, int len /* = 0 */)
  99. {
  100.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  101.     else return itoa((unsigned long)(-i), len, 1, 10);
  102. }
  103. char* dec(unsigned long i, int len /* = 0 */)
  104. {
  105.     return itoa(i, len, 0, 10);
  106. }
  107. char* dec(unsigned int i, int len /* = 0 */)
  108. {
  109.     return itoa(i, len, 0, 10);
  110. }
  111.  
  112. char* hex(long i, int len /* = 0 */)
  113. {
  114.     return itoa((unsigned long)i, len, 0, 16);
  115. }
  116. char* hex(int i, int len /* = 0 */)
  117. {
  118.     return itoa((unsigned long)i, len, 0, 16);
  119. }
  120. char* hex(unsigned long i, int len /* = 0 */)
  121. {
  122.     return itoa(i, len, 0, 16);
  123. }
  124. char* hex(unsigned int i, int len /* = 0 */)
  125. {
  126.     return itoa(i, len, 0, 16);
  127. }
  128.  
  129. char* oct(long i, int len /* = 0 */)
  130. {
  131.     return itoa((unsigned long)i, len, 0, 8);
  132. }
  133. char* oct(int i, int len /* = 0 */)
  134. {
  135.     return itoa((unsigned long)i, len, 0, 8);
  136. }
  137. char* oct(unsigned long i, int len /* = 0 */)
  138. {
  139.     return itoa(i, len, 0, 8);
  140. }
  141. char* oct(unsigned int i, int len /* = 0 */)
  142. {
  143.     return itoa(i, len, 0, 8);
  144. }
  145.